package com.kitty.game.hunpo.service;

import com.kitty.common.cache.RedisService;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.config.NPC;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.hunpo.message.RespHunPoPanelData;
import com.kitty.game.hunpo.message.StringIntValue;
import com.kitty.game.hunqiao.HunQiaoService;
import com.kitty.game.hunqiao.message.HunQiaoCangKu;
import com.kitty.game.hunqiao.model.HunQiaoField;
import com.kitty.game.hunqiao.model.HunQiaoStoreEquipInfo;
import com.kitty.game.hunqiao.model.HunQiaoToSave;
import com.kitty.game.player.model.HunPoProp;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * HunPoService
 *
 * @author StanWind
 * @create 2020 - 04 - 17 1:19 AM
 */
@Service
@Slf4j
public class HunPoService {

    //属性map
    public static final Map<Integer, List<StringIntValue>> PROP_MAP = new HashMap<>();
    //升级所需阴德
    public static final Map<Integer, Integer> UP_REQUIRE_MAP = new HashMap<>();
    //最大阴德
    public static final Integer MAX_YIN_DE = 2000000000;
    @Autowired
    RoleService roleService;
    @Autowired
    RedisService redisService;
    public void loadShenHun(){
        //阶段 影响系数
        float param = 1.0f;
        for (int i = 0; i < 10; i++) {
            int s = i + 1;
            List<StringIntValue> stage = new ArrayList<>();
            /*
             * 这里是魂魄属性  有算法  所有的数字 public class StringIntValue *3  了
             */
            stage.add(new StringIntValue("phy_power", Math.round(param * s * Integer.parseInt(DataCache.REDIS_DATA.get("shenhun_jiacheng_1").toString()))));
            stage.add(new StringIntValue("mag_power", Math.round(param * s * Integer.parseInt(DataCache.REDIS_DATA.get("shenhun_jiacheng_2").toString()))));
            stage.add(new StringIntValue("max_life", Math.round(param * s * Integer.parseInt(DataCache.REDIS_DATA.get("shenhun_jiacheng_3").toString()))));
            stage.add(new StringIntValue("def", Math.round(param * s * Integer.parseInt(DataCache.REDIS_DATA.get("shenhun_jiacheng_4").toString()))));
            stage.add(new StringIntValue("speed", Math.round(param * s * Integer.parseInt(DataCache.REDIS_DATA.get("shenhun_jiacheng_5").toString()))));
            stage.add(new StringIntValue("phy_power", Math.round(param * s * Integer.parseInt(DataCache.REDIS_DATA.get("shenhun_jiacheng_6").toString()))));
            stage.add(new StringIntValue("mag_power", Math.round(param * s * Integer.parseInt(DataCache.REDIS_DATA.get("shenhun_jiacheng_7").toString()))));
            stage.add(new StringIntValue("max_life", Math.round(param * s * Integer.parseInt(DataCache.REDIS_DATA.get("shenhun_jiacheng_8").toString()))));
            stage.add(new StringIntValue("def", Math.round(param * s * Integer.parseInt(DataCache.REDIS_DATA.get("shenhun_jiacheng_9").toString()))));
            stage.add(new StringIntValue("speed", Math.round(param * s * Integer.parseInt(DataCache.REDIS_DATA.get("shenhun_jiacheng_10").toString()))));
            PROP_MAP.put(s, stage);
        }
        //十阶
        for (int i = 0; i < 10; i++) {
            int s = i + 1;
            double value = Math.round(Integer.parseInt(DataCache.REDIS_DATA.get("shenhun_xiaohao").toString()) * s);
            UP_REQUIRE_MAP.put(s, (int) value);
        }
    }

    /**
     * 获取神魂阶增益属性
     *
     * @param level
     * @return
     */
    public List<StringIntValue> getAddProp(int level) {
        return PROP_MAP.get(level);
    }


    /**
     * 升级所需阴德
     *
     * @param level      神魂阶
     * @param levelInner 神魂子阶
     * @return
     */
    public int getStageUpReq(int level, int levelInner) {
        return UP_REQUIRE_MAP.getOrDefault(level, 15);
    }

    /**
     * 加成面板包
     *
     * @param hunPoProp
     */
    public RespHunPoPanelData getPanelPropResp(HunPoProp hunPoProp) {
        RespHunPoPanelData data = new RespHunPoPanelData();
        data.setPhyPower(hunPoProp.getPhyPower());
        data.setMagPower(hunPoProp.getMagPower());
        data.setDefense(hunPoProp.getDefense());
        data.setLife(hunPoProp.getLife());
        data.setSpeed(hunPoProp.getSpeed());
        data.setList(getAddProp(Math.min(hunPoProp.getLevel(), 10)));
        return data;
    }

    /**
     * 发送魂魄面板
     *
     * @param role
     */
    public void sendPanel(Role role) {


        RespHunPoPanelData respHunPoPanelData = getPanelPropResp(getHunPoProp(role));
        HunQiaoService hunQiaoService = SpringUtils.getBean(HunQiaoService.class);
        //魂窍属性显示
        HunQiaoToSave hunQiaoToSave =  hunQiaoService.hunQiaoToSave(role.getRoleId());

        // 显示仓库信息
        //这里跟之前不一样 为什么呢。
        List<HunQiaoStoreEquipInfo> cangKus = hunQiaoToSave.getCangKus();
        HunQiaoCangKu hunQiaoCangKu = new HunQiaoCangKu();
        hunQiaoCangKu.setStr("tyzq_store");
        hunQiaoCangKu.setList(cangKus);
        hunQiaoCangKu.addMore();
        MessagePusher.pushMessage(role, hunQiaoCangKu);


        List<HunQiaoField> hunQiaoFields = hunQiaoService.hunQiaoFields(hunQiaoToSave);
        respHunPoPanelData.setHunQiaoAllProps(hunQiaoFields.stream().map(HunQiaoField::toHunQiaoAllProps).collect(Collectors.toList()));
        SpringUtils.getBean(HunQiaoService.class).sendZhuRu(role,hunQiaoToSave);

        MessagePusher.pushMessage(role, respHunPoPanelData);

        roleService.sendYinDe(role);
    }

    public HunPoProp getHunPoProp(Role role) {
        HunPoProp hunPoProp = role.getExtendBox().getHunPoProp();
        if (hunPoProp == null) {
            //数据初始化
            hunPoProp = new HunPoProp();
            role.getExtendBox().setHunPoProp(hunPoProp);
            hunPoProp.setLevel(1);
            hunPoProp.setLevelInner(0);
            role.save();
        }

        return hunPoProp;
    }


    /**
     * 计算增加属性
     *
     * @param hunPoProp
     */
    public void calcAddedProp(HunPoProp hunPoProp) {
        StringIntValue si = null;
        if (hunPoProp.getLevelInner() == 0) {
            si = getAddProp(hunPoProp.getLevel() - 1).get(9);
        } else {
            si = getAddProp(hunPoProp.getLevel()).get(hunPoProp.getLevelInner() - 1);
        }
        if ("phy_power".equals(si.getPropName())) {
            hunPoProp.setPhyPower(hunPoProp.getPhyPower() + si.getPropValue());
        } else if ("mag_power".equals(si.getPropName())) {
            hunPoProp.setMagPower(hunPoProp.getMagPower() + si.getPropValue());
        } else if ("max_life".equals(si.getPropName())) {
            hunPoProp.setLife(hunPoProp.getLife() + si.getPropValue());
        } else if ("def".equals(si.getPropName())) {
            hunPoProp.setDefense(hunPoProp.getDefense() + si.getPropValue());
        } else if ("speed".equals(si.getPropName())) {
            hunPoProp.setSpeed(hunPoProp.getSpeed() + si.getPropValue());
        }
    }

    public void buyYingde(Role role, String msg, NPC npc) {
        if (msg.equals("购买阴德（6积分=500阴德）")) {
            try {
                roleService.substractChargeScore(role, 6);
            } catch (Exception e) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("积分不足6点！！！"));
                return;
            }
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("扣除积分6点，购买#R500阴德成功！！！"));
            roleService.addYinde(role, 500, true);
        }
        if (msg.equals("购买阴德（500积分=25000阴德）")) {
            try {
                roleService.substractChargeScore(role, 500);
            } catch (Exception e) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("积分不足#R500点！！！"));
                return;
            }
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("扣除积分500点，购买#R25000阴德成功！！！"));
            roleService.addYinde(role, 25000,true);
        }
        //LoggerFunction.ACTIVITY.getLogger().info("buyYingde=={}=={}", role.getUid(), msg);
    }
}
